home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / hardware / summa / test / inputtest.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.7 KB  |  361 lines

  1. #include <stdio.h>
  2. #include <bstring.h>
  3.  
  4. #define _KERNEL
  5. #include <sys/types.h>
  6. #undef _KERNEL
  7. #include <sys/ioctl.h>
  8. #include <sys/errno.h>
  9. #include <sys/termio.h>
  10. #include <sys/cmn_err.h>
  11. #include <sys/fcntl.h>
  12.  
  13. /* Streams stuff */
  14. #include <sys/stream.h>
  15. #include <sys/strids.h>
  16. #include <sys/stropts.h>
  17.  
  18. #include <sys/shmiq.h>
  19. #define _KERNEL
  20. #include <sys/idev.h>
  21.  
  22. #include "inputtest.h"
  23.  
  24. static queue_t    queues[2];
  25. static queue_t    *read_q= &queues[0];
  26. static queue_t    *write_q= &queues[1];
  27.  
  28. int lbolt;
  29. int doass= 1;
  30. unsigned char kbdtype = 0;
  31. static    int    dev_fd;
  32. static    int    pointerMode = 0;
  33.  
  34.     /*
  35.      * This file helps to debug device streams modules from the user
  36.      * level.  
  37.      */
  38.  
  39. /***====================================================================***/
  40.  
  41. void
  42. cmn_err(int level,char *str,void *a1,void *a2,void *a3,void *a4,void *a5)
  43. {
  44.     switch (level) {
  45.     case CE_CONT:     fprintf(stderr,"         ");
  46.             break;
  47.     case CE_WARN:     fprintf(stderr,"Warning! ");
  48.             break;
  49.     case CE_DEBUG:
  50.     default:    break;
  51.     }
  52.     fprintf(stderr,str,a1,a2,a3,a4,a5);
  53.     return;
  54. }
  55.  
  56. void
  57. assfail(char *expr,char *file,int line)
  58. {
  59.     fprintf(stderr,"%s, Line %d: Assertion failed (%s)|n",file,line,expr);
  60.     exit(1);
  61. }
  62.  
  63. mblk_t    *
  64. allocb(int size, uint pri)
  65. {
  66. mblk_t    *blk;
  67.  
  68.     blk= (mblk_t *)malloc(sizeof(mblk_t));
  69.     if (blk!=NULL) {
  70.     blk->b_next= NULL;
  71.     blk->b_prev= NULL;
  72.     blk->b_cont= NULL;
  73.     blk->b_rptr= blk->b_wptr= (unsigned char *)malloc(size);
  74.     if (blk->b_rptr) {
  75.         blk->b_datap= (struct datab *)malloc(sizeof(struct datab));
  76.         if (blk->b_datap) {
  77.         blk->b_datap->db_type= M_DATA;
  78.         return blk;
  79.         }
  80.         free(blk->b_rptr);
  81.     }
  82.     free(blk);
  83.     }
  84.     return NULL;
  85. }
  86.  
  87. /* ARGSUSED */
  88. void
  89. putnext(queue_t *q,mblk_t *blk)
  90. {
  91. struct shmqevent *ev= (struct shmqevent *)blk->b_rptr;
  92.  
  93.     if ((q==read_q)&&(blk->b_datap->db_type==M_DATA)) {
  94.     while ( ((unsigned char *)ev)< blk->b_wptr ) {
  95.         switch (ev->data.type) {
  96.             case QE_PTR_EVENT:
  97.             fprintf(stderr,"Pointer to (%d,%d) ",
  98.                            ev->data.un.ptraxis[0],
  99.                            ev->data.un.ptraxis[1]);
  100.             if (ev->data.flags) {
  101.                 int    some= 0;
  102.                 fprintf(stderr,"[");
  103.                 if (ev->data.flags&QE_RESPONSE)
  104.                 fprintf(stderr,"%sresponse",some++?" ":"");
  105.                 if (ev->data.flags&IDEV_GEN_PTR_X)
  106.                 fprintf(stderr,"%sx",some++?" ":"");
  107.                 if (ev->data.flags&IDEV_GEN_PTR_Y)
  108.                 fprintf(stderr,"%sy",some++?" ":"");
  109.                 if (ev->data.flags&QE_X_CLAMPED)
  110.                 fprintf(stderr,"%sxclamped",some++?" ":"");
  111.                 if (ev->data.flags&QE_Y_CLAMPED)
  112.                 fprintf(stderr,"%syclamped",some++?" ":"");
  113.                 fprintf(stderr,"]\n");
  114.             }
  115.             else fprintf(stderr,"\n");
  116.             break;
  117.             case QE_VAL_EVENT:
  118.             fprintf(stderr,"Valuator %d to %d",ev->data.which,
  119.                                ev->data.un.pos);
  120.             if (ev->data.flags) {
  121.                 int    some= 0;
  122.                 fprintf(stderr,"[");
  123.                 if (ev->data.flags&QE_MORE_EVENTS)
  124.                 fprintf(stderr,"%smore",some++?" ":"");
  125.                 if (ev->data.flags&QE_RESPONSE)
  126.                 fprintf(stderr,"%sresponse",some++?" ":"");
  127.                 if (ev->data.flags&QE_CLAMPED)
  128.                 fprintf(stderr,"%sclamped",some++?" ":"");
  129.                 fprintf(stderr,"]\n");
  130.             }
  131.             else fprintf(stderr,"\n");
  132.             break;
  133.             case QE_BTN_EVENT:
  134.             fprintf(stderr,"Button %d %s\n",ev->data.which,
  135.                 (ev->data.flags&QE_BTN_DOWN)?"down":"up");
  136.             break;
  137.             default:
  138.             fprintf(stderr,"Unknown event type %d\n",ev->data.type);
  139.             break;
  140.         }
  141.         ev++;
  142.     }
  143.     }
  144.     else if (q==write_q) {
  145.     if (blk->b_datap->db_type==M_DATA) {
  146.         fprintf(stderr,"writing %d bytes to device\n",blk->b_wptr-blk->b_rptr);
  147.         write(dev_fd,blk->b_rptr,blk->b_wptr-blk->b_rptr);
  148.     }
  149.     else if (blk->b_datap->db_type==M_IOCTL) {
  150.         struct iocblk *iocb= (struct iocblk *)blk->b_rptr;
  151.         struct strioctl sio;
  152.  
  153.         fprintf(stderr,"ioctl 0x%x",iocb->ioc_count);
  154.         sio.ic_cmd = iocb->ioc_cmd;
  155.         sio.ic_timout = 0;
  156.         if (blk->b_cont) {
  157.         sio.ic_len=    iocb->ioc_count;
  158.         sio.ic_dp=    blk->b_cont->b_rptr;
  159.         fprintf(stderr,"(%d bytes)\n",iocb->ioc_count);
  160.         }
  161.         else {
  162.         sio.ic_len=    0;
  163.         sio.ic_dp=    NULL;
  164.         fprintf(stderr,"(0 (%d) bytes)\n",iocb->ioc_count);
  165.         }
  166.         ioctl(dev_fd,I_STR,&sio);
  167.     }
  168.     }
  169.     else {
  170.     fprintf(stderr,"unknown queue in putnext\n");
  171.     }
  172.     freemsg(blk);
  173. }
  174.  
  175. void
  176. freemsg(mblk_t *blk)
  177. {
  178. mblk_t *next;
  179.    if (blk!=NULL) {
  180.     do {
  181.         next = blk->b_cont;
  182.         if (blk->b_rptr!=NULL)
  183.         free(blk->b_rptr);
  184.         if (blk->b_datap!=NULL)
  185.         free(blk->b_datap);
  186.         free(blk);
  187.         blk= next;
  188.     } while (blk);
  189.    }
  190.    return;
  191. }
  192.  
  193.  
  194. /***====================================================================***/
  195.  
  196. void
  197. flushq(register queue_t *q, int flag)
  198. {
  199. }
  200.  
  201. int
  202. canput(queue_t *q)
  203. {
  204.     return 1;
  205. }
  206.  
  207. void
  208. qreply(queue_t *q,mblk_t *blk)
  209. {
  210. }
  211.  
  212. int
  213. itimeout(int (*func)(),void *priv,int ticks,pl_t id)
  214. {
  215.     return 1;
  216. }
  217.  
  218. void
  219. untimeout( int id )
  220. {
  221. }
  222.  
  223. toid_t
  224. bufcall(uint size, int pri, void (*func)(), long arg)
  225. {
  226.     return 0;
  227. }
  228.  
  229. splstr()
  230. {
  231.     return 0;
  232. }
  233.  
  234. void *
  235. kmem_alloc(size_t nbytes,int flag)
  236. {
  237.     return malloc(nbytes);
  238. }
  239.  
  240. void
  241. kmem_free(void *vptr, size_t size)
  242. {
  243.     free(vptr);
  244. }
  245.  
  246. #undef WR
  247. queue_t *
  248. WR(queue_t *q)
  249. {
  250.     return(q+1);
  251. }
  252.  
  253.  
  254.     /*
  255.      *  Add stubs for any functions modules used by your streams module
  256.      *  here.
  257.      */
  258.  
  259. /***====================================================================***/
  260.  
  261.     /*
  262.      * To add a device:
  263.      *     Add an extern for the streamtab which describes the device
  264.      *         and add an entry to devices[].  The name of the device
  265.      *       is the name of the streams module *not* the name returned
  266.      *       by your IDEVGETDEVICEDESC ioct.   For example, the spaceball
  267.      *       device is named "spaceball," but the object file for it
  268.      *         is "sball.o" and the streams module name is "sball" --
  269.      *       the spaceball entry in the table lists "sball."
  270.      *     To simplify debugging, you might want to change the 
  271.      *       DEFAULT_DEVICE to be the name of your device.
  272.      */
  273.  
  274. #define    DEFAULT_DEVICE    "summa"
  275.  
  276. extern    struct streamtab summainfo;
  277.  
  278. static struct _devinfo {
  279.     char            *name;
  280.     struct streamtab    *st;
  281. } devices[] = {
  282.     {    "summa",    &summainfo    },
  283.     {    NULL,        NULL        }
  284. };
  285.  
  286. /***====================================================================***/
  287.  
  288. int
  289. openDevice(int    argc,char *argv[])
  290. {
  291. int    i;
  292. char    buf[128];
  293. char    *name= DEFAULT_DEVICE;
  294. int    (*openfunc)();
  295.  
  296.     if (argc>1)
  297.     name= argv[1];
  298.     for (i=0;i<sizeof(devices)/sizeof(struct _devinfo);i++) {
  299.     if ((devices[i].name!=NULL)&&
  300.         (strcasecmp(devices[i].name,name)==0)) {
  301.         openfunc= devices[i].st->st_rdinit->qi_qopen;
  302.         if ((*openfunc)(read_q,0,0,MODOPEN)==0) {
  303.             if (argc>2) {
  304.             dev_fd = open(argv[2],O_RDWR|O_NONBLOCK);
  305.             if (dev_fd<0) {
  306.                 fprintf(stderr,"Couldn't open %s\n",argv[2]);
  307.                 return -1;
  308.             }
  309.             }
  310.             sprintf(buf,"/dev/input/%s",name);
  311.             dev_fd = open(buf,O_RDWR|O_NONBLOCK);
  312.             if (dev_fd<0) {
  313.             sprintf(buf,"/dev/input/%s-pointer",name);
  314.             dev_fd = open(buf,O_RDWR|O_NONBLOCK);
  315.             pointerMode = 1;
  316.             }
  317.             return dev_fd;
  318.         }
  319.         else fprintf(stderr,"open function for %s failed\n",name);
  320.     }
  321.     }
  322.     fprintf(stderr,"device %s not found\n",name);
  323.     return -1;
  324. }
  325.  
  326. /***====================================================================***/
  327.  
  328. int
  329. main(int argc,char *argv[])
  330. {
  331. char    buf[128];
  332. int    n,found;
  333. void    (*readfunc)( idevInfo *, char *, int );
  334. int    (*wioctl)( idevInfo *, int, int, char *, int *);
  335. idevInfo    *pInfo;
  336.  
  337.     if ((dev_fd=openDevice(argc,argv))<0) {
  338.     return 1;
  339.     }
  340.     pInfo= (idevInfo *)read_q->q_ptr;
  341.     readfunc= pInfo->sInfo.readData;
  342.     wioctl= pInfo->sInfo.writeIoctl;
  343.     (*wioctl)(pInfo,IDEVINITDEVICE,0,NULL,&found);
  344.     if (pointerMode) {
  345.     idevPtrMode mode;
  346.     mode.mode=    IDEV_GEN_ALL_EVENTS|IDEV_EXCLUSIVE;
  347.     mode.xAxis=    0;
  348.     mode.yAxis=    1;
  349.     (*wioctl)((idevInfo *)read_q->q_ptr,IDEVSETPTRMODE,
  350.                 sizeof(idevPtrMode),(unsigned char *)&mode,
  351.                 &found);
  352.     }
  353.     while (1) {
  354.     n= read(dev_fd,buf,128);
  355.     if (n>0) {
  356.         (*readfunc)( (idevInfo *)read_q->q_ptr, buf, n );
  357.     }
  358.     }
  359. }
  360.  
  361.